package cn.edu.sgu.www.mhxysy.service.role.impl;

import cn.edu.sgu.www.mhxysy.dto.role.RoleAttributeUpdateDTO;
import cn.edu.sgu.www.mhxysy.dto.role.RoleJiadianDTO;
import cn.edu.sgu.www.mhxysy.dto.role.RoleJiadianRenameDTO;
import cn.edu.sgu.www.mhxysy.dto.role.RoleJiadianUpdateDTO;
import cn.edu.sgu.www.mhxysy.entity.role.RoleAttribute;
import cn.edu.sgu.www.mhxysy.entity.role.RoleJiadian;
import cn.edu.sgu.www.mhxysy.enums.OpenStatus;
import cn.edu.sgu.www.mhxysy.exception.GlobalException;
import cn.edu.sgu.www.mhxysy.mapper.role.RoleAttributeMapper;
import cn.edu.sgu.www.mhxysy.mapper.role.RoleJiadianMapper;
import cn.edu.sgu.www.mhxysy.restful.ResponseCode;
import cn.edu.sgu.www.mhxysy.service.role.RoleJiadianService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Slf4j
@Service
public class RoleJiadianServiceImpl implements RoleJiadianService {

	private final RoleJiadianMapper roleJiadianMapper;
	private final RoleAttributeMapper roleAttributeMapper;

	@Autowired
	public RoleJiadianServiceImpl(
			RoleJiadianMapper roleJiadianMapper,
			RoleAttributeMapper roleAttributeMapper) {
		this.roleJiadianMapper = roleJiadianMapper;
		this.roleAttributeMapper = roleAttributeMapper;
	}

	@Override
	public void rename(RoleJiadianRenameDTO renameDTO) {
		UpdateWrapper<RoleJiadian> updateWrapper = new UpdateWrapper<>();

		updateWrapper.set("name", renameDTO.getName());

		updateWrapper.eq("role_id", renameDTO.getRoleId());
		updateWrapper.eq("jiadian_schema", renameDTO.getJiadianSchema());

		/*
		 * update role_jiadian set name = #{name}
		 * where role_id = #{roleId} and jiadian_schema = #{jiadianSchema}
		 */
		int update = roleJiadianMapper.update(updateWrapper.getEntity(), updateWrapper);

		if (update < 1) {
			log.debug("操作失败，受影响的行数：{}", update);

			throw new GlobalException(ResponseCode.BAD_REQUEST, "操作失败");
		}
	}

	@Override
	public void enable(String roleId, Integer jiadianSchema) {
		// 1、关闭角色所有加点
		UpdateWrapper<RoleJiadian> updateWrapper = new UpdateWrapper<>();

		updateWrapper.eq("role_id", roleId);
		updateWrapper.set("open_status", OpenStatus.UN_OPEN.getValue());

		// 通过角色id修改角色的加点方案开启状态为0-未开启
		roleJiadianMapper.update(updateWrapper.getEntity(), updateWrapper);

		// 2、开启指定角色加点
		updateWrapper.clear();

		updateWrapper.eq("role_id", roleId);
		updateWrapper.eq("jiadian_schema", jiadianSchema);

		updateWrapper.set("open_status", OpenStatus.OPEN.getValue());

		// 通过角色id和加点方案修改开启状态为1-已开启
		int update = roleJiadianMapper.update(updateWrapper.getEntity(), updateWrapper);

		if (update != 1) {
			log.debug("操作失败，受影响的行数：{}", update);

			throw new GlobalException(ResponseCode.BAD_REQUEST, "操作失败");
		}
	}

	@Override
	public void unlock(String roleId, Integer jiadianSchema) {
		// 通过角色ID和加点方案修改解锁状态
		UpdateWrapper<RoleJiadian> updateWrapper = new UpdateWrapper<>();

		updateWrapper.eq("role_id", roleId);
		updateWrapper.eq("jiadian_schema", jiadianSchema);
		updateWrapper.eq("open_status", OpenStatus.UN_OPEN.getValue());

		updateWrapper.set("open_status", OpenStatus.OPEN.getValue());

		int update = roleJiadianMapper.update(updateWrapper.getEntity(), updateWrapper);

		if (update != 1) {
			log.debug("操作失败，受影响的行数：{}", update);

			throw new GlobalException(ResponseCode.BAD_REQUEST, "解锁失败，请稍后重试~");
		}
	}

	@Override
	public void update(RoleJiadianUpdateDTO jiadianUpdateDTO, RoleAttributeUpdateDTO attributeUpdateDTO) {
		// 通过角色ID和加点方案查询角色加点
		QueryWrapper<RoleJiadian> queryWrapper = new QueryWrapper<>();

		queryWrapper.eq("role_id", jiadianUpdateDTO.getRoleId());
		queryWrapper.eq("jiadian_schema", jiadianUpdateDTO.getJiadianSchema());

		RoleJiadian jiadian = roleJiadianMapper.selectOne(queryWrapper);

		if (jiadian == null) {
			throw new GlobalException(ResponseCode.BAD_REQUEST, "角色加点不存在！");
		}

		String jiadianId = jiadian.getId();

		// 1、修改角色加点
		RoleJiadian roleJiadian = new RoleJiadian();

		roleJiadian.setId(jiadianId);
		roleJiadian.setMoli(jiadianUpdateDTO.getMoli());
		roleJiadian.setNaili(jiadianUpdateDTO.getNaili());
		roleJiadian.setTizhi(jiadianUpdateDTO.getTizhi());
		roleJiadian.setMinjie(jiadianUpdateDTO.getMinjie());
		roleJiadian.setLiliang(jiadianUpdateDTO.getLiliang());

		roleJiadianMapper.updateById(roleJiadian);

		// 2、修改角色属性
		UpdateWrapper<RoleAttribute> updateWrapper = new UpdateWrapper<>();

		RoleAttribute roleAttribute = new RoleAttribute();

		roleAttribute.setMofa(attributeUpdateDTO.getMofa());
		roleAttribute.setWlsh(attributeUpdateDTO.getWlsh());
		roleAttribute.setWlfy(attributeUpdateDTO.getWlfy());
		roleAttribute.setFssh(attributeUpdateDTO.getFssh());
		roleAttribute.setFsfy(attributeUpdateDTO.getFsfy());
		roleAttribute.setSudu(attributeUpdateDTO.getSudu());
		roleAttribute.setQixue(attributeUpdateDTO.getQixue());

		updateWrapper.eq("jiadian_id", jiadianId);
		updateWrapper.eq("role_id", jiadian.getRoleId());

		roleAttributeMapper.update(roleAttribute, updateWrapper);
	}

	@Override
	public List<RoleJiadian> selectByRoleId(String roleId) {
		QueryWrapper<RoleJiadian> queryWrapper = new QueryWrapper<>();

		queryWrapper.eq("role_id", roleId);

		return roleJiadianMapper.selectList(queryWrapper);
	}

	@Override
	public RoleJiadian selectJiadian(RoleJiadianDTO jiadianDTO) {
		QueryWrapper<RoleJiadian> queryWrapper = new QueryWrapper<>();

		queryWrapper.eq("role_id", jiadianDTO.getRoleId());
		queryWrapper.eq(
				jiadianDTO.getOpenStatus() != null,
				"open_status", jiadianDTO.getOpenStatus()
		);
		queryWrapper.eq(
				jiadianDTO.getUnlockStatus() != null,
				"unlock_status", jiadianDTO.getUnlockStatus()
		);
		queryWrapper.eq(
				jiadianDTO.getJiadianSchema() != null,
				"jiadian_schema", jiadianDTO.getJiadianSchema()
		);

		return roleJiadianMapper.selectOne(queryWrapper);
	}

}